package com.clz.hyperit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.clz.hyperit.entity.Goods;
import com.clz.hyperit.entity.Order;
import com.clz.hyperit.service.GoodsService;
import com.clz.hyperit.util.FileUploadUtil;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.GoodsOrder;

@RestController
@RequestMapping(value = "/goods")
public class GoodsController {
	private ResultMessage result = null;
	@Autowired
	private GoodsService goodsService;
	private final Logger logger = LoggerFactory.getLogger(GoodsController.class);
	/**
	 * 获得商品列表
	 * @return
	 */
	@RequestMapping(value = "/findGoods", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findGoods(){
		result = new ResultMessage();
		List<Goods> goodslist = goodsService.findGoods();
		result.setResultInfo("获取商品成功");
		result.setServiceResult(true);
		result.getResultParm().put("goodslist",goodslist);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 添加商品
	 * @param goods
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveGoods", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> savePosition(Goods goods,MultipartFile file,HttpServletRequest request){
		result = new ResultMessage();
		if(goodsService.getByName(goods.getName())!=null){
			result.setResultInfo("已存在该商品");
			result.setServiceResult(false);
		}
		else{
			if(goods.getCost()==null) goods.setCost(0);
			if(goods.getStock()==null) goods.setStock(0);
			if (!file.isEmpty()) {
				logger.info("file不为空，开始处理上传头像");
				String goodspath = FileUploadUtil.NewFileUpload(request, file,"goodsimg");
				logger.info("上传头像访问地址："+ goodspath);
				goods.setPath(goodspath);
			} 
			else{
				goods.setPath("");
			}
			goodsService.saveGoods(goods);
			result.setResultInfo("添加商品成功");
			result.setServiceResult(true);
		}
		Goods goods_vo = goodsService.getByName(goods.getName());
		result.getResultParm().put("goods",goods_vo);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 删除商品
	 * @param goods
	 * @return
	 */
	@RequestMapping(value = "/deleteGoods", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deletePosition(Goods goods){
		result = new ResultMessage();
		goodsService.deleteGoods(goods);
		result.setResultInfo("删除商品成功");
		result.setServiceResult(true);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 更新商品信息
	 * @param goods
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateGoods", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updatePosition(Goods goods,MultipartFile file,HttpServletRequest request){
		result = new ResultMessage();
		if (file!=null) {
			logger.info("file不为空，开始处理上传头像");
			String goodspath = FileUploadUtil.NewFileUpload(request, file,"goodsimg");
			logger.info("上传头像访问地址："+ goodspath);
			goods.setPath(goodspath);
		} 
		goodsService.updateGoods(goods);
		result.setResultInfo("更新商品成功");
		result.setServiceResult(true);
		result.getResultParm().put("goods_id",goods.getId());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 得到某个商品信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getonegoods", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findGoods(Integer id){
		result = new ResultMessage();
		Goods goods = goodsService.getByID(id);
		if(goods==null){
			result.setServiceResult(false);
			result.setResultInfo("没有这个商品");
		}
		else{
			result.setResultInfo("获取商品成功");
			result.setServiceResult(true);
			result.getResultParm().put("goods",goods);
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 购买商品->下订单
	 * @return
	 */
	@RequestMapping(value = "/buyGoods", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> buyGoods(Goods goods,Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		//result.setServiceResult(true);
		Order order = goodsService.saveOrder(goods,userdetail_id);
		if(order==null){
			result.setResultInfo("购买失败，库存不足或者hb不足");
			result.setServiceResult(false);
		}
		else{
			result.setResultInfo("购买成功");
			result.getResultParm().put("order",order);
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 查找个人订单信息
	 * @return
	 */
	@RequestMapping(value = "/findPersonOrder", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findPersonOrder(Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<GoodsOrder> goodsorder = goodsService.findPersonOrder(userdetail_id);
		result.setResultInfo("获取个人订单成功");
		result.getResultParm().put("goodsorder",goodsorder);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	/**
	 * 查找个人订单信息
	 * @return
	 */
	@RequestMapping(value = "/findOrder", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findGoodsOrder(){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<GoodsOrder> goodsorder = goodsService.findOrder();
		result.setResultInfo("获取所有订单成功");
		result.getResultParm().put("goodsorder",goodsorder);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}
